﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OneMake.DataAccess;
using OneMake.Model;
namespace OneMake.Winforms
{
    public partial class uc_report_kf_detail : UserControl, IBaseReportCtrl
    {
        private DataHelper dataHelper = new DataHelper();

        private int selShopId = 0;              //当前店铺
        private DateTime selBeginTime;          //开始时间
        private DateTime selEndTime;            //结束时间

        public uc_report_kf_detail()
        {
            InitializeComponent();
        }

        private void uc_report_kf_detail_Load(object sender, EventArgs e)
        {
            dtpBegin.Value = DateTime.Now.AddDays(-7);
            dtpEnd.Value = DateTime.Now;
            LoadShops();
        }
        
        private void LoadShops()
        {
            cbShops.DataSource = dataHelper.GetShops();
            cbShops.DisplayMember = "name";
            cbShops.ValueMember = "shopid";
            if (cbShops.Items.Count > 0)
                cbShops.SelectedIndex = 0;
        }

        public string ReportName
        {
            get { return "report_kf_detail"; }
        }

        public DockStyle Dock2 
        {
            get { return this.Dock; }
            set { this.Dock = value; }
        }

        public Control Parent2
        {
            get { return this.Parent; }
            set { this.Parent = value; }
        }

        public void LoadData() 
        {
            lbProgress.Visible = true;
            panel1.Refresh();

            this.dgvReport.DataSource = null;
            this.dgvReport.Refresh();

            if (cbShops.SelectedValue != null)
                selShopId = System.Convert.ToInt32(cbShops.SelectedValue.ToString());
            else
                selShopId = 0;

            selBeginTime = dtpBegin.Value;
            selEndTime = dtpEnd.Value;

            Dictionary<string, string> attrMap = dataHelper.GetTableFieldMap(ReportName);

            dgvReport.Columns.Clear();
            foreach (KeyValuePair<string, string> item in attrMap)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn();
                col.Name = item.Key;
                col.HeaderText = item.Value;
                col.DataPropertyName = item.Key;
                dgvReport.Columns.Add(col);
            }

            this.dgvReport.DataSource = get_report_kf_details(selShopId, selBeginTime, selEndTime);
            lbProgress.Visible = false;
        }


        public List<report_kf_detail> get_report_kf_detail(int shopid, DateTime day)
        {
            string sql = "";
            List<report_kf_detail> reports = new List<report_kf_detail>();

            using (var db = new PetaPoco.Database("mysql" + UserSession.Network))
            {
                //ct_d_jxdb
                sql = string.Format(@"SELECT a.stat_datetime as stat_datetime, a.ww as ww, a.n_serve as jxdb_n_serve,
                    a.n_inquire as jxdb_n_inquire,a.n_ordered as jxdb_n_ordered,a.n_paid as jxdb_n_paid,
                    a.n_ordered_amount as jxdb_n_ordered_amount,a.n_paid_amount as jxdb_n_paid_amount,
                    a.p_paid_amount as jxdb_p_paid_amount FROM ct_d_jxdb a WHERE a.shopid={0} AND a.stat_datetime='{1}' AND a.ww is not null",shopid, day.Date);

                foreach (report_kf_detail r in db.Query<report_kf_detail>(sql))
                {
                    //ct_d_quire
                    sql = string.Format(@"SELECT b.n_quire as quire_n_quire,b.n_lost as quire_n_lost,b.n_order as quire_n_order,
                        b.n_order_amount as quire_n_order_amount,b.n_final_order as quire_n_final_order,
                        b.n_final_order_amount as quire_n_final_order_amount,b.per_success as quire_per_success
                        FROM ct_d_quire b WHERE b.shopid={0} AND b.stat_datetime='{1}' AND b.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.quire_n_quire = t.quire_n_quire;
                            r.quire_n_lost = t.quire_n_lost;
                            r.quire_n_order = t.quire_n_order;
                            r.quire_n_order_amount = t.quire_n_order_amount;
                            r.quire_n_final_order = t.quire_n_final_order;
                            r.quire_n_final_order_amount = t.quire_n_final_order_amount;
                            r.quire_per_success = t.quire_per_success;
                        }
                    }


                    //ct_d_order
                    sql = string.Format(@"SELECT c.n_order as order_n_order,c.n_order_amount as order_n_order_amount,
                            c.n_lost as order_n_lost,c.n_lost_amount as order_n_lost_amount,c.n_paid as order_n_paid,
                            c.n_paid_amount as order_n_paid_amount,c.n_final_paid as order_n_final_paid,c.n_final_paid_amount as order_n_final_paid_amount,
                            c.per_success as order_per_success FROM ct_d_order c WHERE c.shopid={0} AND c.stat_datetime='{1}' AND c.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.order_n_order = t.order_n_order;
                            r.order_n_order_amount = t.order_n_order_amount;
                            r.order_n_lost = t.order_n_lost;
                            r.order_n_lost_amount = t.order_n_lost_amount;
                            r.order_n_paid = t.order_n_paid;
                            r.order_n_paid_amount = t.order_n_paid_amount;
                            r.order_n_final_paid = t.order_n_final_paid;
                            r.order_n_final_paid_amount = t.order_n_final_paid_amount;
                            r.order_per_success = t.order_per_success;
                        }
                    }

                    //ct_d_kdjdb
                    sql = string.Format(@"SELECT d.uprice as kdjdb_uprice,d.valid_uprice as kdjdb_valid_uprice,d.paid_uprice as kdjdb_paid_uprice 
                        FROM ct_d_kdjdb d WHERE d.shopid={0} AND d.stat_datetime='{1}' AND d.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.kdjdb_uprice = t.kdjdb_uprice;
                            r.kdjdb_valid_uprice = t.kdjdb_valid_uprice;
                            r.kdjdb_paid_uprice = t.kdjdb_paid_uprice; 
                        }
                    }

                    //ct_d_kjsdb
                    sql = string.Format(@"SELECT e.n_order as kjsdb_n_order,e.n_order_ok as kjsdb_n_order_ok,
                            e.n_unit as kjsdb_n_unit,e.n_unit_ok as kjsdb_n_unit_ok,e.n_avg_price as kjsdb_n_avg_price,
                            e.n_avg_price_ok as kjsdb_n_avg_price_ok 
                        FROM ct_d_kjsdb e WHERE e.shopid={0} AND e.stat_datetime='{1}' AND e.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.kjsdb_n_order = t.kjsdb_n_order;
                            r.kjsdb_n_order_ok = t.kjsdb_n_order_ok;
                            r.kjsdb_n_unit = t.kjsdb_n_unit;
                            r.kjsdb_n_unit_ok = t.kjsdb_n_unit_ok;
                            r.kjsdb_n_avg_price = t.kjsdb_n_avg_price;
                            r.kjsdb_n_avg_price_ok = t.kjsdb_n_avg_price_ok;
                        }
                    }

                    //ct_d_cgldb
                    sql = string.Format(@"SELECT f.per_iq_todayorder as cgldb_per_iq_todayorder,f.per_iq_finalorder as cgldb_per_iq_finalorder,
                        f.per_iq_finalpaid as cgldb_per_iq_finalpaid,f.per_todaypaid as cgldb_per_todaypaid,f.per_finalpaid as cgldb_per_finalpaid 
                        FROM ct_d_cgldb f WHERE f.shopid={0} AND f.stat_datetime='{1}' AND f.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.cgldb_per_iq_todayorder = t.cgldb_per_iq_todayorder;
                            r.cgldb_per_iq_finalorder = t.cgldb_per_iq_finalorder;
                            r.cgldb_per_iq_finalpaid = t.cgldb_per_iq_finalpaid;
                            r.cgldb_per_todaypaid = t.cgldb_per_todaypaid;
                            r.cgldb_per_finalpaid = t.cgldb_per_finalpaid;
                        }
                    }

                    //ct_d_gzldb
                    sql = string.Format(@"SELECT g.n_reception as gzldb_n_reception,
                        g.n_msgs as gzldb_n_msgs,g.n_customermsgs as gzldb_n_customermsgs,
                        g.n_csmsgs as gzldb_n_csmsgs,g.n_cswords as gzldb_n_cswords,
                        g.n_maxreception as gzldb_n_maxreception,g.per_wwreply as gzldb_per_wwreply,
                        g.avg_wwanswer as gzldb_avg_wwanswer,g.n_firstanswer as gzldb_n_firstanswer 
                        FROM ct_d_gzldb g WHERE g.shopid={0} AND g.stat_datetime='{1}' AND g.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.gzldb_n_reception = t.gzldb_n_reception;
                            r.gzldb_n_msgs = t.gzldb_n_msgs;
                            r.gzldb_n_customermsgs = t.gzldb_n_customermsgs;
                            r.gzldb_n_csmsgs = t.gzldb_n_csmsgs;
                            r.gzldb_n_cswords = t.gzldb_n_cswords;
                            r.gzldb_n_maxreception = t.gzldb_n_maxreception;
                            r.gzldb_per_wwreply = t.gzldb_per_wwreply;
                            r.gzldb_avg_wwanswer = t.gzldb_avg_wwanswer;
                            r.gzldb_n_firstanswer = t.gzldb_n_firstanswer; 
                        }
                    }


                    //ct_d_tkqkdb
                    sql = string.Format(@"SELECT h.n_quit_count as tkqkdb_n_quit_count,
                        h.n_quit_amount as tkqkdb_n_quit_amount,h.n_quit_person as tkqkdb_n_quit_person,
                        h.n_quit_money as tkqkdb_n_quit_money,h.n_done_count as tkqkdb_n_done_count,
                        h.n_done_amount as tkqkdb_n_done_amount,h.n_done_person as tkqkdb_n_done_person,h.n_done_money as tkqkdb_n_done_money 
                        FROM ct_d_tkqkdb h WHERE h.shopid={0} AND h.stat_datetime='{1}' AND h.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.tkqkdb_n_quit_count = t.tkqkdb_n_quit_count;
                            r.tkqkdb_n_quit_amount = t.tkqkdb_n_quit_amount;
                            r.tkqkdb_n_quit_person = t.tkqkdb_n_quit_person;
                            r.tkqkdb_n_quit_money = t.tkqkdb_n_quit_money;
                            r.tkqkdb_n_done_count = t.tkqkdb_n_done_count;
                            r.tkqkdb_n_done_amount = t.tkqkdb_n_done_amount;
                            r.tkqkdb_n_done_person = t.tkqkdb_n_done_person;
                            r.tkqkdb_n_done_money = t.tkqkdb_n_done_money; 
                        }
                    }

                    //ct_d_tkqkdb
                    sql = string.Format(@"SELECT i.n_recps as comment_n_recps,
                        i.n_send as comment_n_send,i.n_recv as comment_n_recv,i.n_rank_a as comment_n_rank_a,
                        i.n_rank_b as comment_n_rank_b,i.n_rank_c as comment_n_rank_c,i.n_rank_d as comment_n_rank_d,
                        i.per_send as comment_per_send,i.per_recv as comment_per_recv,i.per_good as comment_per_good,i.n_good as comment_n_good 
                        FROM ct_d_comment i WHERE i.shopid={0} AND i.stat_datetime='{1}' AND i.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.comment_n_recps = t.comment_n_recps;
                            r.comment_n_send = t.comment_n_send;
                            r.comment_n_recv = t.comment_n_recv;
                            r.comment_n_rank_a = t.comment_n_rank_a;
                            r.comment_n_rank_b = t.comment_n_rank_b;
                            r.comment_n_rank_c = t.comment_n_rank_c;
                            r.comment_n_rank_d = t.comment_n_rank_d;
                            r.comment_per_send = t.comment_per_send;
                            r.comment_per_recv = t.comment_per_recv;
                            r.comment_per_good = t.comment_per_good;
                            r.comment_n_good = t.comment_n_good; 
                        }
                    }

                    //ct_d_kfghzb
                    sql = string.Format(@"SELECT j.shopname as kfghzb_shopname,j.fx as kfghzb_fx,j.sqsh as kfghzb_sqsh,j.kfname as kfghzb_kfname,j.sfgh as kfghzb_sfgh FROM ct_d_kfghzb j 
                        WHERE j.shopid={0} AND j.stat_datetime='{1}' AND j.ww='{2}'", shopid, day.Date, r.ww);
                    using (var dbc = new PetaPoco.Database("mysql" + UserSession.Network))
                    {
                        report_kf_detail t = dbc.FirstOrDefault<report_kf_detail>(sql);
                        if (t != null)
                        {
                            r.kfghzb_shopname = t.kfghzb_shopname;
                            r.kfghzb_fx = t.kfghzb_fx;
                            r.kfghzb_sqsh   = t.kfghzb_sqsh;
                            r.kfghzb_kfname = t.kfghzb_kfname;
                            r.kfghzb_sfgh   = t.kfghzb_sfgh;
                        }
                    }

                    reports.Add(r);
                }
            }

            return reports;
        }

        public List<report_kf_detail> get_report_kf_details(int shopid, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            List<report_kf_detail> reports = new List<report_kf_detail>();
            DateTime tempTime = beginTime;
            do
            {
                reports.AddRange(get_report_kf_detail(shopid, tempTime));
                tempTime = tempTime.AddDays(1);
            }
            while (tempTime <= endTime);

            return reports;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Tools.ExportToExcel.ExportToExcel exp = new Tools.ExportToExcel.ExportToExcel();
            sfdExportToxcel.FileName = "客服绩效明细报表.xlsx";
            sfdExportToxcel.Filter = "Excel表格(*.xlsx)|*.xlsx|Todos los Archivos (*.*)|*.*";
            sfdExportToxcel.FilterIndex = 1;
            sfdExportToxcel.RestoreDirectory = true;

            if (sfdExportToxcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = sfdExportToxcel.FileName;
                try
                {
                    exp.dataGridView2Excel(this.dgvReport, path);
                    MessageBox.Show("导出完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            } 
        }

    }
}
